/**  
 * @Title: CommonController.java
 * @Package com.xzit.tmms.controller
 * @Description: TODO
 * @author 董亮亮
 * @date 2016年6月27日 上午8:19:24
 * @version V1.0  
 */
package com.xzit.tmms.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xzit.tmms.common.base.BaseController;
import com.xzit.tmms.common.constant.PathConstant;
import com.xzit.tmms.common.exception.UtilException;
import com.xzit.tmms.common.util.CommonUtil;

/**
 * @ClassName: CommonController
 * @Description: TODO
 * @author 董亮亮
 * @date 2016年6月27日 上午8:19:24
 */
@Controller
@RequestMapping("")
public class CommonController extends BaseController {

	@RequestMapping("/downloadFile")
	public String downloadFile(HttpServletResponse response, String fileName, String name) throws UtilException {
		if (CommonUtil.isEmpty(name)) {
			name = fileName;
		}
		response.setCharacterEncoding("utf-8");
		response.setContentType("multipart/form-data");
		response.setHeader("Content-Disposition",
				"attachment;fileName=" + fileName);

		// 读取文件
		try {
			File file = new File(PathConstant.fileAbsPath + fileName);
			InputStream inputStream = new FileInputStream(file);
			OutputStream os = response.getOutputStream();
			byte[] b = new byte[1024];
			int length;
			while ((length = inputStream.read(b)) > 0) {
				os.write(b, 0, length);
			}
			inputStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}
